package com.pro.controller;

import com.pro.mapper.CouponUsedMapper;
import com.pro.pojo.Coupon;
import com.pro.pojo.CouponUsed;
import com.pro.pojo.JsBeforeUser;
import com.pro.pojo.JsonData;
import com.pro.service.CouponUsedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * auther 杨沛琪
 * createDate 2022/1/12
 */
@Controller
@RequestMapping("/CouponUsed")
public class CouponUsedController {
    @Autowired
    private CouponUsedService couponUsedService;

    //获取用户使用优惠券记录
    @RequestMapping("/getTable")
    @ResponseBody
    /*
    * @author Ypq
    * @date 2022/1/12 15:11
    * @return com.pro.pojo.CouponUsed
    */
    public JsonData getTable(){
        List<JsBeforeUser> getTable = couponUsedService.getTable();
        return getTable != null ? JsonData.buildSuccess(getTable) : JsonData.buildError("服务端错误");
    }
    //获取用户使用优惠券记录
    @RequestMapping("/getTableByAnUsedState")
    @ResponseBody
    /*
    * @author Ypq
    * @date 2022/1/13 20:05
    * @return com.pro.pojo.CouponUsed
    */
    public JsonData getTableByAnUsedState(Integer userId, Integer usedState){
        List<String> tableByAnUsedState = couponUsedService.getTableByAnUsedState(userId, usedState);
        return tableByAnUsedState != null ? JsonData.buildSuccess(tableByAnUsedState) : JsonData.buildError("服务端错误");
    }
    //查询已过期
    @RequestMapping("/getTableByEndDate")
    @ResponseBody
    /*
    * @author Ypq
    * @date 2022/1/13 20:53
    * @return com.pro.pojo.CouponUsed
    */

    public JsonData getTableByEndDate(Integer userId) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式
        List<String> tableByEndDate = couponUsedService.getTableByEndDate(userId);//传进来的结束时间数据
        Integer count=0;
        //遍历获取到的数据
        for (String s : tableByEndDate) {
            Date data=simpleDateFormat.parse(s); //把数据转换成对象
            if(data.getTime()<new Date().getTime()){//对象的毫秒值跟当前时间对象的毫秒值进行比较
                count++;//小于当前时间对象的毫秒值就加一
            }
        }
        return count != null ? JsonData.buildSuccess(count) : JsonData.buildError("服务端错误");
    }
    //筛选查询
    @RequestMapping("/queryCouponList")
    @ResponseBody
    /*
    * @author Ypq
    * @date 2022/1/12 16:06
    * @return com.pro.pojo.CouponUsed
    */
    public JsonData queryCouponList(String account,String nickname){
        List<CouponUsed> couponUseds = couponUsedService.queryCouponUsed(account, nickname);
        return couponUseds != null ? JsonData.buildSuccess(couponUseds) : JsonData.buildError("服务端错误");
    }

    //添加优惠券使用记录
    @RequestMapping("/addCouponUsed")
    @ResponseBody
    /*
    * @author Ypq
    * @date 2022/1/14 9:31
    * @return com.pro.pojo.CouponUsed
    */
    public JsonData addCouponUsed(CouponUsed couponUsed,Coupon coupon){
        Boolean addUserCouponUsed = null;
        try {
            addUserCouponUsed = couponUsedService.addUserCouponUsed(couponUsed, coupon);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return addUserCouponUsed?JsonData.buildSuccess(addUserCouponUsed):JsonData.buildError("添加失败");
    }
}
